package Engine.Model
{
	import Engine.Model.LogPrint;
	
	import Interface.Model.ILoaderManager;
	
	import flash.events.Event;

	public class LoaderManager implements ILoaderManager
	{
		private static const MAX_LOADER_NUM:int = 1;
		private static const LOAD_TYPE_SWF:int = 1;
		private static const LOAD_TYPE_TEXT:int = 2;
		private static const LOAD_TYPE_MAGIC:int = 3;
		private static var CurrentLoadNum:int = 0;
		
		private var _LoadList:Array = new Array;
		private var _resMgr:ResManager;
		private var _isLoading:Boolean;
		
		public function LoaderManager()
		{
			this._resMgr = new ResManager();
			this._isLoading = false;
		}
		
		public function loadImage(path:String, onCompleteFunc:Function, onProgressFunc:Function=null, onIOErrorFunc:Function=null, onSecurityErrorFunc:Function=null):void
		{
			if(path == null || path == "")
			{
				if(onIOErrorFunc != null)
					onIOErrorFunc(null);
				return;
			}
			if(onIOErrorFunc == null)
				this._LoadList.push([LoaderManager.LOAD_TYPE_MAGIC,path,onCompleteFunc,onProgressFunc,onError,onError]);
			else
				this._LoadList.push([LoaderManager.LOAD_TYPE_MAGIC,path,onCompleteFunc,onProgressFunc,onIOErrorFunc,onError]);
		}
		
		public function loadText(path:String, onCompleteFunc:Function, onProgressFunc:Function=null, onIOErrorFunc:Function=null, onSecurityErrorFunc:Function=null):void
		{
			if(path == null || path == "")
			{
				if(onIOErrorFunc != null)
					onIOErrorFunc(null);
				return;
			}
			if(onIOErrorFunc == null)
				this._LoadList.push([LoaderManager.LOAD_TYPE_TEXT,path,onCompleteFunc,onProgressFunc,onError,onError]);
			else
				this._LoadList.push([LoaderManager.LOAD_TYPE_TEXT,path,onCompleteFunc,onProgressFunc,onIOErrorFunc,onError]);
		}
		
		public function loadSWF(path:String,onCompleteFunc:Function, onProgressFunc:Function=null, onIOErrorFunc:Function=null, onSecurityErrorFunc:Function=null):void
		{
			if(path == null || path == "")
			{
				if(onIOErrorFunc != null)
					onIOErrorFunc(null);
				return;
			}
			if(onIOErrorFunc == null)
				this._LoadList.push([LoaderManager.LOAD_TYPE_SWF,path,onCompleteFunc,onProgressFunc,onError,onError]);
			else
				this._LoadList.push([LoaderManager.LOAD_TYPE_SWF,path,onCompleteFunc,onProgressFunc,onIOErrorFunc,onError]);
		}
		
		public function startloading():void
		{
			if(this._isLoading)
				return;
			this._isLoading = true;
			this.loadNext();
		}
		
		private static function setCurrentNum(isAdd:Boolean):void
		{
			if(isAdd)
				CurrentLoadNum++;
			else
				CurrentLoadNum--;
		}
		
		private function loadNext():void
		{
			if(LoaderManager.CurrentLoadNum >= LoaderManager.MAX_LOADER_NUM)
				return;
			if(this._LoadList.length <= 0)
			{
				this._isLoading = false;
				return;
			}
			LoaderManager.setCurrentNum(true);
			this.load(this._LoadList.shift());
			this.loadNext();
		}
		
		private function load(ar:Array):void
		{
			var loadType:int = ar.shift();
			switch(loadType)
			{
				case LoaderManager.LOAD_TYPE_SWF:
					this.doLoadSWF(ar);
					break;
				case LoaderManager.LOAD_TYPE_TEXT:
					this.doloadText(ar);
					break;
				case LoaderManager.LOAD_TYPE_MAGIC:
					this.doloadImage(ar);
					break;
				default:
					break;
			}
		}
		
		private function doLoadSWF(ar:Array):void
		{
			var backFun:Function = ar[1];
			var path:String = ar[0];
			this._resMgr.loadSWF(ar[0],completeFun,ar[2],ar[3],ar[4]);
			function completeFun(p_e:Event) : void
			{
				backFun(p_e,path);
				LoaderManager.setCurrentNum(false);
				loadNext();
			}
		}
		
		private function doloadImage(ar:Array):void
		{
			var backFun:Function = ar[1];
			this._resMgr.loadImage(ar[0],completeFun,ar[2],ar[3],ar[4]);
			function completeFun(obj:Object, path:String) : void
			{
				backFun(obj,path);
				LoaderManager.setCurrentNum(false);
				loadNext();
			}
		}
		
		private function doloadText(ar:Array):void
		{
			var backFun:Function = ar[1];
			var path:String = ar[0];
			this._resMgr.loadText(ar[0],completeFun,ar[2],ar[3],ar[4]);
			function completeFun(obj:Object) : void
			{
				backFun(obj,path);
				LoaderManager.setCurrentNum(false);
				loadNext();
			}
		}
		
		private function onError(p_e : Event) : void
		{
			LogPrint.instance().printLog(p_e.toString());
		}
	}
}